package com.cd2cd.code_template.util.vertify.controller;


import com.cd2cd.code_template.util.vertify.model.dto.UserValidateDto;
import com.cd2cd.code_template.util.vertify.model.vo.ImageVerificationVo;
import com.cd2cd.code_template.util.vertify.service.CaptchaService;
import com.cd2cd.code_template.vo.BaseRes;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import javax.validation.groups.Default;

/**
 * @Description: 验证码
 * -------------------
 * @Author: YangXingfu
 * @Date: 2019/07/24 17:01
 */

@Slf4j
@Controller
@RequestMapping("/captcha")
public class CaptchaController {


    /**
     * 运算码业务处理对象
     */
    @Autowired
    private CaptchaService captchaService;


    /**
     * 获取验证码
     * @param userValidateDto 验证码参数  username,type
     * @return 根据类型参数返回验证码
     */
    @PostMapping(value = "/get/verification/image",consumes = "application/json")
    @ResponseBody
    public BaseRes<ImageVerificationVo> getVerificationImage(@RequestBody @Validated({UserValidateDto.ApplyImage.class, UserValidateDto.VerifyCode.class,Default.class})
        UserValidateDto userValidateDto  , BindingResult bindingResult) {
//        validData(bindingResult);
        return captchaService.selectImageVerificationCode(userValidateDto);
    }

    /**
     * 校验验证码
     * @param userValidateDto username,type,answer
     */
    @ResponseBody
    @PostMapping(value = "/check/verification/result",consumes = "application/json")
    public  BaseRes<String> checkVerificationResult(@RequestBody @Validated({UserValidateDto.ApplyImage.class, UserValidateDto.VerifyCode.class, Default.class})
            UserValidateDto userValidateDto, BindingResult bindingResult) {
//        validData(bindingResult);
        return captchaService.checkVerificationResult(userValidateDto);
    }

    /**
     * 获取TOTP Secret_key
     * @param userValidateDto  username,applyType
     */
    @ResponseBody
    @PostMapping(value = "/genUserSecretKey",consumes = "application/json")
    public  BaseRes<String> genUserSecretKey(@RequestBody @Validated({ UserValidateDto.ApplyKey.class, Default.class})
            UserValidateDto userValidateDto, BindingResult bindingResult) {
//        validData(bindingResult);
        return captchaService.genUserSecretKey(userValidateDto);
    }

    private void validData(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuffer sb = new StringBuffer();
            for (ObjectError error : bindingResult.getAllErrors()) {
                sb.append(error.getDefaultMessage());
            }
            throw new ValidationException(sb.toString());
        }
    }
}